
const state = {
  currentTag: {
    title: '工作台',
    link: '/dashboard'
  },
  tagsList: [{
    title: '工作台',
    link: '/dashboard'
  }]
}

const mutations = {
  add(state, pload) {
    state.currentTag = pload
    if (!state.tagsList.some(i => i.link === pload.link)) { state.tagsList.push(pload) }
  },
  del(state, pload) {
    let currentIndex
    state.tagsList.forEach((i, j) => { if (i.link === pload.link) currentIndex = j })
    state.tagsList.splice(currentIndex, 1)
    state.currentTag = state.tagsList[state.tagsList.length - 1]
  },
  closeLeft(state) {
    let currentIndex
    state.tagsList.forEach((i, j) => { if (i.link === state.currentTag.link) currentIndex = j })
    state.tagsList.splice(1, currentIndex - 1)
  },
  closeRight(state) {
    let currentIndex
    state.tagsList.forEach((i, j) => { if (i.link === state.currentTag.link) currentIndex = j })
    state.tagsList.splice(currentIndex + 1)
  },
  closeRest(state) {
    if (state.currentTag.link !== '/dashboard') {
      state.tagsList = [{
        title: '工作台',
        link: '/dashboard'
      }, state.currentTag]
    } else {
      state.tagsList = [{
        title: '工作台',
        link: '/dashboard'
      }]
    }
  },
  closeAll(state) {
    state.currentTag = {
      title: '工作台',
      link: '/dashboard'
    }
    state.tagsList = [{
      title: '工作台',
      link: '/dashboard'
    }]
  }
}

export default {
  namespaced: true,
  state,
  mutations
}

